package net.lbtech.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Administrator on 2016/8/5.
 * 规范与体脂秤蓝牙通讯的格式
 */
public class WifiDataFormatUtil {
    protected static Logger logger = LoggerFactory.getLogger(WifiDataFormatUtil.class);
    //红檬体脂秤蓝牙Sevice的UUID开头
    public static final String BLE_WRITE_STARTWITH = "0000fff1"; //体脂秤蓝牙设备写入模块
    public static final String BLE_NOTIFY_STARTWITH = "0000fff4"; //体脂秤蓝牙设备消息模块
    //命令开头
    public static final int COMMAND_WIFI_HEAD = 0xF5;           //Wifi通讯固定开头
    public static final byte COMMAND_IAQ_CONFIG = 0x01;          //设置空气监控仪
    public static final byte COMMAND_CITY_WEATHER = (byte)0x82;     //设置当地城市空气状况
    public static final byte COMMAND_WEIGHT_DATA = 0x03;        //空气监控仪上传监测数据

    public static final byte RIGHT_RETURN = 0x00;        //正确
    public static final byte ERROR_RETURN = 0x01;        //错误


    /**
     * 设置空气监控仪
     * 命令格式： 0xF5 0x01 AA BB CC DD EE FF GG HH II JJ KK LL MM OO PP QQ 00 CRC
     * 功能：向空气监控仪写入设置
     * 描述：AA:年， BB:月， CC:日， DD：时， EE:分  FF,GG:PM2.5超标警告值， GG,II:甲醛超标警告值，  JJ：蜂鸣器器开（01）关（00）     KK,LL,MM,OO,PP,QQ: WIFI模块的MAC地址
     * 命令回复：
     * 校验正确且执行OK返回：	0xF5 0x81 0x00 00 00 00 00 00 00 00 00 00 00 00 00 CRC
     * 校验错误或执行Fail返回：	0xF5 0x81 0x01 00 00 00 00 00 00 00 00 00 00 00 00 CRC
     * @param time           时间
     * @param  pm25_alarm PM2.5超标警告值
     * @param  formaldehyde_alarm 甲醛超标警告值
     * @param  buzzerSwitch：蜂鸣器器开（01）关（00）
     *
     * @return  返回一个处理好的空气检测仪设置
     */
    public static byte[] iaqConfig(byte[] time,byte[] pm25_alarm,byte[] formaldehyde_alarm,byte buzzerSwitch){
        //总共发送18个字节
        byte data[] = new byte[20];
        int crc = 0;   //校验位
        //初始数组
        for(int i=0;i<data.length;i++){
            data[i] = 0;
        }
        data[0] = (byte) COMMAND_WIFI_HEAD;
        data[1] = COMMAND_IAQ_CONFIG;
        data[2] = RIGHT_RETURN;
        data[3] = time[0];
        data[4] = time[1];
        data[5] = time[2];
        data[6] = time[3];
        data[7] = time[4];
        data[8] = pm25_alarm[0];
        data[9] = (byte)(pm25_alarm[1]-128);
        data[10] = formaldehyde_alarm[0];
        data[11] = (byte)(formaldehyde_alarm[1]-128);
        data[12] = buzzerSwitch;
        for(int i=0;i<19;i++){ //CRC是前面15位的相加
            crc += data[i];
        }
        data[19] = (byte)(crc & 0x7F);
        logger.debug( "userData2byte: crc" + crc );
        for(int i=0;i<data.length;i++){
            logger.debug( i + " --> " +  data[i]);
        }
        return data;
    }

    /**
     * 返回当地城市空气状况
     * 命令格式： 0xF5 0x82 0x00 AA BB CC DD EE FF GG HH II JJ KK 00 CRC
     * 功能：向体脂秤写入体重单位
     * 描述：AA：天气
     * BB：温度正负(00负  01正）
     * CC:温度
     * DD：湿度
     * EE, FF：PM2.5
     * GG：空气质量
     * HH,II,JJ,KK：所属城市
     * @param
     * @return
     */
    public static byte[] cityWeather(byte[] weather,byte[] city){
        //总共发送20个字节
        byte data[] = new byte[20];
        int crc = 0;   //校验位
        //初始数组
        for(int i=0;i<data.length;i++){
            data[i] = 0;
        }
        data[0] = (byte) COMMAND_WIFI_HEAD;
        data[1] = COMMAND_CITY_WEATHER;
        data[2] = RIGHT_RETURN;
        data[3] = weather[0];//天气
        data[4] = weather[1];//温度正负
        data[5] = weather[2];//温度
        data[6] = weather[3];//湿度
        data[7] = 0;
        data[8] = weather[4];//PM25
        data[9] = weather[5];//空气质量
        data[10] = city[0];
        data[11] = city[1];
        data[12] = city[2];
        data[13] = city[3];
        for(int i=0;i<19;i++){
            crc += data[i];
        }
        data[19] = (byte)(crc & 0x7F);
        logger.debug( "userData2byte: crc" + crc );
        for(int i=0;i<data.length;i++){
            logger.debug( i + " --> " +  data[i]);
        }
        return data;
    }

    /**
     * 错误返回
     * 0xF5 0x83 0x01 00 00 00 00 00 00 00 00 00 00 00 00 CRC
     */
    public static byte[] errorReturn(byte responseCode){
        byte[] data = new byte[20];
        int crc = 0;   //校验位
        data[0] = (byte) COMMAND_WIFI_HEAD;
        data[1] = responseCode;
        data[2] = 0x01;
        for(int i=0;i<19;i++){
            crc += data[i];
        }
        data[19] = (byte)(crc & 0x7F);
        return data;
    }

    /**
     * 正确返回
     * 0xF5 0x83 0x01 00 00 00 00 00 00 00 00 00 00 00 00 CRC
     */
    public static byte[] trueReturn(byte responseCode){
        byte[] data = new byte[20];
        int crc = 0;   //校验位
        data[0] = (byte) COMMAND_WIFI_HEAD;
        data[1] = responseCode;
        data[2] = 0x00;
        for(int i=0;i<19;i++){
            crc += data[i];
        }
        data[19] = (byte)(crc & 0x7F);
        return data;
    }

}
